﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Gadgets.Accountings
{
	public partial class eCashNettedInquiry : WeifenLuo.WinFormsUI.Docking.DockContent  
	{
		public eCashNettedInquiry()
		{
			InitializeComponent();
		}
		
		private void eCashNettedInquiry_Load(object sender, EventArgs e)
		{
			try
			{
                
                ABSCommonLibrary.CreateGridView(ref gridControl1);
                colTRADING_DATE.FormatSpecifier = DeclareLibrary.DATE_DISPLAY_FORMAT;
                colNET_DATE.FormatSpecifier = DeclareLibrary.DATE_DISPLAY_FORMAT;
                colDEBIT_QTTY.FormatSpecifier = DeclareLibrary.NUMBER_FORMAT;
                colCREDIT_QTTY.FormatSpecifier = DeclareLibrary.NUMBER_FORMAT;
                colCREDIT_CASH.FormatSpecifier = DeclareLibrary.CURRENCY_FORMAT;
                colDEBIT_CASH.FormatSpecifier = DeclareLibrary.CURRENCY_FORMAT;
                DataSet tran_type = ABSCommonLibrary.DoInquiry(
                            CommonLibrary.CommonLibrary.BOSC014002, new object[3] { 5, 0, 0 });
                DataSet tran_type1 = tran_type.Copy();
                comboBoxEditor1.SetDataBinding(tran_type1, tran_type1.Tables[0].TableName);
                comboBoxEditor1.ValueMember = "CODE";
                comboBoxEditor1.DisplayFormat = "%DESCRIPTION%";
                gridControl1.Columns["NET_TYPE"].CellEditorManager = comboBoxEditor1;

                comboBoxViewer1.SetDataBinding(tran_type, tran_type.Tables[0].TableName);
                comboBoxViewer1.ValueMember = "CODE";
                comboBoxViewer1.DisplayFormat = "%DESCRIPTION%";
                gridControl1.Columns["NET_TYPE"].CellViewerManager = comboBoxViewer1;

                switch (this.Tag.ToString())
                {
                    case "00":
                        insertionRow1.Visible = false;
                        dataRowTemplate1.ReadOnly = true;
                        break;

                    case "02":
                        insertionRow1.Visible = true;
                        dataRowTemplate1.ReadOnly = false;
                        break;
                    default:
                        break;
                }
			}
			catch (Exception ex)
			{
				ABSCommonLibrary.ShowErrorMessage(ex.ToString());
			}
		}
		private void load_grid()
		{
			DateTime trading_date = DeclareLibrary.MARKET_DAY;			
			string account_no = string.Empty;			
			string status = string.Empty;
			int trans_type = 0;
			
            if (valueRow1.Cells["ACCOUNT_NO"].Value != null)
                account_no = valueRow1.Cells["ACCOUNT_NO"].Value.ToString();
            if (valueRow1.Cells["TRADING_DATE"].Value != null)
                trading_date = Convert.ToDateTime(valueRow1.Cells["TRADING_DATE"].Value);
            if (valueRow1.Cells["STATUS"].Value != null)
                status = valueRow1.Cells["STATUS"].Value.ToString();
            if (valueRow1.Cells["NET_TYPE"].Value != null)
                trans_type =Convert.ToInt16( valueRow1.Cells["NET_TYPE"].Value);

            DataSet dsData = ABSCommonLibrary.DoInquiry(
                CommonLibrary.CommonLibrary.BOSC016003, new object[4]{
                trading_date, trans_type, account_no, status});
            gridControl1.SetDataBinding(dsData, dsData.Tables[0].TableName);
		}

		private void UpdateNettingTrans(DataRow row)
		{			
			object[] p_param = new object[4];
            p_param[0] = row["NET_ID"];
            p_param[1] = row["NET_DATE"];
            p_param[2] = DeclareLibrary.MARKET_DAY;
			p_param[3] = DeclareLibrary.USER_NAME;
			int error_code = Convert.ToInt16(ABSCommonLibrary.DoExecute(
                CommonLibrary.CommonLibrary.BOSC016001, ref p_param));
            if (error_code == 0)
			    ABSCommonLibrary.ShowMessage("Xac nhan giao dich thanh toan bu tru thanh cong!");

		}

        private void eCashNettedInquiry_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.KeyCode == Keys.F11)
                {
                    if (((DataSet)gridControl1.DataSource).Tables[0].GetChanges() != null)
                    {
                        foreach (DataRow row in ((DataSet)gridControl1.DataSource).Tables[0].GetChanges().Rows)
                        {
                            if (row.RowState == DataRowState.Modified)
                            {
                                UpdateNettingTrans(row);
                            }
                        }
                        load_grid();
                    }
                }
                else if (e.KeyCode == Keys.F9)
                {
                    load_grid();
                }
                else if (e.KeyCode == Keys.F10)
                {
                    SaveFileDialog saveFileDialog = new SaveFileDialog();
                    saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
                    saveFileDialog.Filter = "Excel Files (*.xls)|*.xls";
                    saveFileDialog.AddExtension = true;
                    if (saveFileDialog.ShowDialog(this) == DialogResult.OK)
                    {
                        string FileName = saveFileDialog.FileName;
                        // TODO: Add code here to save the current contents of the form to a file.
                        Xceed.Grid.Exporting.ExcelExporter export = new Xceed.Grid.Exporting.ExcelExporter();
                        export.Export(gridControl1, FileName);
                    }
                }
            }
            catch (Exception ex)
            {
                ABSCommonLibrary.ShowErrorMessage(ex.ToString());
            }
        }

	}
}